#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QToolButton>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->setMouseTracking(true);
    ui->setupUi(this);
    InitUI();
    InitData();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitUI()
{
    //去除窗口的边框
    this->setWindowFlags(Qt::FramelessWindowHint |
                         Qt::WindowSystemMenuHint |
                         Qt::WindowMinMaxButtonsHint);

    SetWindowCloseAndMax();

    //设置窗口大小
    // this->setFixedSize(1222, 768);

    //设置背景色 white
    QPalette palette(this->palette());
    palette.setColor(QPalette::Background, Qt::white);
    this->setPalette(palette);

    if(!LoadImg(LINE_PATH, ui->lineLabel))
        return ;

    if(!LoadImg(TOPBK_PATH, ui->topBkLabel))
        return ;

    LoadImgBt(BT_PREV_PATH, QSize(18, 25), ui->prevBt);
    LoadImgBt(BT_PLAY_PATH, QSize(57, 21),  ui->playBt);
    LoadImgBt(BT_NEXT_PATH, QSize(105, 25),  ui->nextBt);
}

void MainWindow::InitData()
{
    m_bDrag = false;
    m_pos = QPoint(0, 0);
}

void MainWindow::SetWindowCloseAndMax()
{
    int wide = width();//获取界面的宽度
    //构建最小化、最大化、关闭按钮
    QToolButton *minButton = new QToolButton(this);
    QToolButton *closeButton= new QToolButton(this);
    QToolButton *maxButton= new QToolButton(this);
    //获取最小化、关闭按钮图标
    QPixmap minPix  = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
    //设置最小化、关闭按钮图标
    minButton->setIcon(minPix);
    closeButton->setIcon(closePix);
    maxButton->setIcon(maxPix);
    //设置最小化、关闭按钮在界面的位置
    minButton->setGeometry(wide-65,5,20,20);
    closeButton->setGeometry(wide-25,5,20,20);
    maxButton->setGeometry(wide-45,5,20,20);
    //设置鼠标移至按钮上的提示信息
    minButton->setToolTip(tr("最小化"));
    closeButton->setToolTip(tr("关闭"));
    maxButton->setToolTip(tr("最大化"));
    //设置最小化、关闭按钮的样式
    minButton->setStyleSheet("background-color:transparent;");
    closeButton->setStyleSheet("background-color:transparent;");
    maxButton->setStyleSheet("background-color:transparent;");
    connect(closeButton, SIGNAL(clicked()), this, SLOT(Close()) );
    connect(minButton, SIGNAL(clicked()), this, SLOT(Min()));
    //connect(maxButton, SIGNAL(clicked()), this, SLOT(Max()));
}

bool MainWindow::LoadImg(const QString &qstrFileName, QLabel * pLabel)
{
    bool bRet = true;
    QImage* qImg=new QImage;

    if(! ( qImg->load(qstrFileName) ) ) //加载图像
    {
        QMessageBox::information(this,
                                 tr("<温馨提示>"),
                                 tr("打开图像失败!"));
        bRet = false;
    }

    if(bRet)
        pLabel->setPixmap(QPixmap::fromImage(*qImg));
    pLabel->setAutoFillBackground(true);

    delete qImg;

    return bRet;
}

bool MainWindow::LoadImgBt(const QString &qstrFileName, const QSize & qSwh, QToolButton *pBt)
{
    QPixmap qIcon(qstrFileName);

    pBt->setIcon(qIcon);
    pBt->setIconSize(qIcon.size());
    pBt->setStyleSheet(QString("background: transparent;"));
    pBt->setFixedSize(qIcon.size());
    pBt->setGeometry(qSwh.width(), qSwh.height(), qIcon.width(), qIcon.height());

    return true;
}


void MainWindow::mousePressEvent(QMouseEvent *pEvent)
{
    if(pEvent->button() == Qt::LeftButton)
    {
        m_bDrag = true;
        m_pos = pEvent->globalPos() - this->pos();
        pEvent->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *pEvent)
{
    if(m_bDrag && (pEvent->buttons() & Qt::LeftButton))
    {
        this->move(pEvent->globalPos() - m_pos);
        pEvent->accept();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *pEvent)
{
    if(pEvent->button() == Qt::LeftButton)
        m_bDrag = false;
}

void MainWindow::Close()
{
    this->close();
}

void MainWindow::Min()
{
    this->showMinimized();
}

void MainWindow::Max()
{
    //this->showMaximized();
}

void MainWindow::on_prevBt_clicked()
{
    QMessageBox::information(this,
                             tr("<温馨提示>"),
                             tr("prev!"));
}

void MainWindow::on_playBt_clicked()
{
    QMessageBox::information(this,
                             tr("<温馨提示>"),
                             tr("play!"));
}

void MainWindow::on_nextBt_clicked()
{
    QMessageBox::information(this,
                             tr("<温馨提示>"),
                             tr("next!"));
}
